#!/usr/bin/python

# Copyright (c) 2009 Thomas P Jakobsen <tpj@cs.au.dk>.
#
# This file is part of CSearch.
#
# CSearch is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# CSearch is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CSearch. If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import re

"""Delete various temporary files in the project directories."""

pattern = r'.*\.pyc|.*~|.*\.orig|.*\.rej'

def traverse(basedir):
    for dirname, dirshere, fileshere in os.walk(basedir):
        for filename in fileshere:
            if re.match(pattern, filename):
                pathname = os.path.join(dirname, filename)
                print "Deleting %s" % pathname
                os.remove(pathname)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        basedir = '.'
    else:
        basedir = sys.argv[1]
    traverse(basedir)
